﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNViewFormatTemplateWorker
	{
		
		public static void  formatTemplate(View view)
		{
			XmlElement root = view.Doc.DocumentElement;
			string xslPrefix = view.XslPrefix;
			string templateFilter = "//" + NamespaceUtil.toFullName(xslPrefix, "template");
			XmlNodeList templateXmlNodeList = DOMUtil.selectNodes(root, templateFilter, 0, view.NsResolver);
			foreach (XmlElement templateElement in templateXmlNodeList)
			{
				string matchBinding = getTemplateMatchBinding(view, templateElement);
				foreach (XmlNode childNode in templateElement.ChildNodes)
				{
					if (childNode is XmlElement)
					{
						XmlElement element = (XmlElement) childNode;
						if( !element.LocalName.Equals( "if" ) ){
							element.SetAttribute(JetConstant.HTML_id, matchBinding);
						}else{
							foreach( XmlNode subNode in element.ChildNodes ){
								if( subNode is XmlElement ){
									XmlElement subElement = (XmlElement) subNode;
									subElement.SetAttribute( JetConstant.HTML_id , matchBinding );
								}
							}
							formatTemplateIf( view, templateElement, element );
						}
					}
				}
			}
		}

		private static void formatTemplateIf( View view, XmlElement templateElement, XmlElement ifElement)
		{
			string test = ifElement.GetAttribute( "test" );

			//CID 0507 //2010.03.01 //Comment
			/*
			string xslPrefix = view.XslPrefix;
			string attrName = NamespaceUtil.toFullName( xslPrefix, "attribute" );
			string chooseName = NamespaceUtil.toFullName( xslPrefix, "choose" );
			
			foreach( XmlNode subNode in ifElement.ChildNodes )
			{
				if( subNode is XmlElement )
				{
					XmlElement element = (XmlElement) subNode;
					XmlElement cloneElement = (XmlElement)element.CloneNode( true );
						
					//CID 0104 //2008.08.31 //Begin
					//检察是否有<xsl:attribute name="style">,如果有直接将if-test作为最后的子元素添加.
					string styleAttributeFilter = NamespaceUtil.toFullName( view.XslPrefix, "attribute" ) + "[@name='style']";
					XmlElement styleAttributeElement = DOMUtil.selectANode( cloneElement, styleAttributeFilter, view.NsResolver ) as XmlElement;
					if( styleAttributeElement != null )
					{
						XmlElement ifTestElement = view.Doc.CreateElement( xslPrefix, "if", JetConstant.URI_XSL );
						ifTestElement.SetAttribute( "test", "not(" + test + ")" );
						ifTestElement.InnerText = "DISPLAY: none";

						styleAttributeElement.AppendChild( ifTestElement );
					}
					//CID 0104 //2008.08.31 //End
					else
					{
						string style = element.GetAttribute( "style" );
						if( style.Length > 0 )
						{
							style = style + ";";
						}

						XmlElement attrElement = view.Doc.CreateElement( xslPrefix, "attribute", JetConstant.URI_XSL );
						attrElement.SetAttribute( "name", "style" );
					
						XmlText styleValueText = view.Doc.CreateTextNode( style );
						attrElement.AppendChild( styleValueText );

						XmlElement ifTestElement = view.Doc.CreateElement( xslPrefix, "if", JetConstant.URI_XSL );
						ifTestElement.SetAttribute( "test", "not(" + test + ")" );
						ifTestElement.InnerText = "DISPLAY: none";
						attrElement.AppendChild( ifTestElement );

						XmlElement firstElement = DOMUtil.findFirstChildElement( cloneElement );
						if( firstElement != null )
						{
							cloneElement.InsertBefore( attrElement, firstElement );
						}
						else
						{
							cloneElement.AppendChild( attrElement );
						}
					}

					templateElement.InsertBefore( cloneElement, ifElement );
				}
			}
			templateElement.RemoveChild( ifElement );
			*/
			//CID 0507 //2010.03.01 //Comment
			
			if( view.Manifest.ConditionFormatTestList == null )
			{
				view.Manifest.ConditionFormatTestList = new ArrayList();
			}
			view.Manifest.ConditionFormatTestList.Add( test );
		}
		
		private static string getTemplateMatchBinding(View view, XmlElement templateElement)
		{
			string match = templateElement.GetAttribute("match");
			if (match.Length <= 0)
			{
				throw new InfoJetException("template no attribute match", "XVFT01");
			}
            //CID 0714 //2010.11.29 //Begin
			string binding = DOMUtil.getSectionBindingExpr( view, match );
            //CID 0714 //2010.11.29 //End
			return binding;
		}
	}
}